<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_setup_report extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('keu_setup_report_model', 'SetupReport');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Report Keuangan";
		viewByTemplate('keu_setup_report/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->SetupReport->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("kd_report") == '') {
			$error->Add("kd_report", "Kode Report", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM keu_setup_report WHERE kd_report = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kd_report"))))."'")) > 0) {
			$error->Add("kd_report", "Kode Report", $tpl_duplicate);
		}

		if (post("nama_report") == '') {
			$error->Add("nama_report", "Nama Report", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kd_report', 'nama_report', 'tipe_report'));
				$data['kd_report']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kd_report'])));
				
				if (post("mode") == "create")
				{
					$data['create_date']	= NOW;
					$data['create_user']	= USERID;

					$this->SetupReport->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.kd_report ?? ""));

				}
				else if (post("mode") == "edit")
				{
					$data['modify_date']	= NOW;
					$data['modify_user']	= USERID;
					
					$this->SetupReport->Update($data, 
										array('kd_report'	=> $data['kd_report']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.kd_report` ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kd_report	= get('kd_report');
		
		if ($kd_report != "")
		{
			// Delete Single
			$ls_result = $this->SetupReport->DeleteSingle($kd_report);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->SetupReport->DeleteSingle($row['kd_report']);
			}
		}
		
		echo $ls_result;
	}
}
?>